import { MessageBox } from 'element-stm';
import col from '../private/columnMap';

let areaInfo = {};

/**
 * 计算两个float数字之和
 * f1: float
 * f2: float
 * pow: init default 2
*/
function floatSum(f1, f2, pow = 2) {
  const m = 10 ** pow;
  return (Math.round(f1 * m) + Math.round(f2 * m)) / m;
}

/**
 * 计算区域内的一个字段金额之和
 * area:    Array   area data
 * colName: String   column name
*/
function sumColumn4Area(area, colName) {
  let sum = 0;
  if (area instanceof Array && area.length) {
    for (const data of area) {
      sum = floatSum(sum, parseFloat(data[colName].LABEL));
    }
  }
  return sum;
}

/**
 * 计算区域内的一个字段金额根据另一字段分组的和
 * area:      Array    area data
 * colName:   String   column name
 * groupName: String   group column name
*/
function sumColumnGroup4Area(area, colName, groupName) {
  const group = {};
  if (area instanceof Array && area.length) {
    for (const data of area) {
      const id = data[groupName].LABEL;
      const value = parseFloat(data[colName].LABEL);
      if (group[id]) {
        group[id] = floatSum(group[id], value);
      } else {
        group[id] = value;
      }
    }
  }
  return group;
}

/**
 * 获取一个区域的名称
 * area String 区域代码
*/
function getAreaName(area) {
  return areaInfo[area].AREA_NAME;
}
/**
 * 获取一个列的名称
 * column String 列代码
*/
function getColumnName(column) {
  const area = column.split('_')[0];
  const columnArray = areaInfo[area].AREA_COLUMNS;
  for (const columnObj in columnArray) {
    if (columnObj === column) {
      return columnArray[columnObj].COL_NAME;
    }
  }
  return '未知字段';
}

function showMessageBox(message) {
  MessageBox(message, '校验失败', {
    confirmButtonText: '确定',
    // callback: action => {
    //   Message({
    //     type: 'error',
    //     message: `action: ${ action }`,
    //   });
    // },
  });
}

const validate = {
  validationRequired(data) {
    let flag = true;
    let lendType =false;
    let lendTypeN =false;
    let repaySR01 =false;
    let repaySR02 =false;
    // 遍历每个区
    for (const area in areaInfo) {
      const columnArray = areaInfo[area].AREA_COLUMNS;
      // 遍历区的每一列
      for (const columnObj in columnArray) {
        if (columnArray[columnObj].COL_REQUIRED === 'true') {
          // 遍历当前区的所有数据确保必填项填写
          for (const row in data[area]) {
            data[area][row][columnObj].STATUS = true;
            if (!data[area][row][columnObj].LABEL) {
              flag = false;
            }
          }
        } else if (columnArray[columnObj].COL_CODE === col.mainItem04) {
          // 交易机构必填
          for (const row in data[area]) {
            if (!data[area][row][col.mainItem04].VALUE || data[area][row][col.mainItem04].VALUE === '[]') {
              flag = false;
              item04 = true;
              break;
            }
          }
        } else if (columnArray[columnObj].COL_CODE === col.lendItem05 || columnArray[columnObj].COL_CODE === col.lendItem15 || columnArray[columnObj].COL_CODE === col.lendDzBillNo) {
          for (const row in data[area]) {
            if (data[area][row][col.lendLendType].budgetFlag === 'Y' && 
            ((!data[area][row][col.lendItem05].VALUE || data[area][row][col.lendItem05].VALUE === '' || data[area][row][col.lendItem05].VALUE === '[]') ||
            (!data[area][row][col.lendItem15].VALUE || data[area][row][col.lendItem15].VALUE === '' || data[area][row][col.lendItem15].VALUE === '[]'))) {
              // 借款单类型返回Y 组织机构和预算科目必填
              flag = false;
              lendType = true;
              break;
            } else if (data[area][row][col.lendLendType].budgetFlag === 'N' &&
            ((data[area][row][col.lendDzBillNo].VALUE && data[area][row][col.lendDzBillNo].VALUE !== '' && data[area][row][col.lendDzBillNo].VALUE !== '[]') ||
            (data[area][row][col.lendItem05].VALUE && data[area][row][col.lendItem05].VALUE !== '' && data[area][row][col.lendItem05].VALUE !== '[]') ||
            (data[area][row][col.lendItem15].VALUE && data[area][row][col.lendItem15].VALUE !== '' && data[area][row][col.lendItem15].VALUE !== '[]'))) {
              // 借款单类型返回N 组织机构和预算科目必填
              flag = false;
              lendTypeN = true;
              break;
            }
          }
        } else if (columnArray[columnObj].COL_CODE === col.repayItem04 || columnArray[columnObj].COL_CODE === col.repayItem06 || columnArray[columnObj].COL_CODE === col.repayItem05 || columnArray[columnObj].COL_CODE === col.repayItem01) {
          for (const row in data[area]) {
            if (data[area][row][col.repayItem08] && data[area][row][col.repayItem08].code === 'SR02' && (
              (!data[area][row][col.repayItem01].VALUE || data[area][row][col.repayItem01].VALUE === '' || data[area][row][col.repayItem01].VALUE === '[]') ||
              (data[area][row][col.repayItem04].VALUE && data[area][row][col.repayItem04].VALUE !== '' && data[area][row][col.repayItem04].VALUE !== '[]') ||
              (data[area][row][col.repayItem06].VALUE && data[area][row][col.repayItem06].VALUE !== '' && data[area][row][col.repayItem06].VALUE !== '[]') ||
              (data[area][row][col.repayItem05].VALUE && data[area][row][col.repayItem05].VALUE !== '' && data[area][row][col.repayItem05].VALUE !== '[]'))) {
              // 还款单类型返回SR01 
              flag = false;
              repaySR02 = true;
              break;
            } else if (data[area][row][col.repayItem08] && data[area][row][col.repayItem08].code === 'SR01' && (
              (data[area][row][col.repayItem01].VALUE && data[area][row][col.repayItem01].VALUE !== '' && data[area][row][col.repayItem01].VALUE !== '[]') ||
              (!data[area][row][col.repayItem04].VALUE || data[area][row][col.repayItem04].VALUE === '' || data[area][row][col.repayItem04].VALUE === '[]') ||
              (!data[area][row][col.repayItem06].VALUE || data[area][row][col.repayItem06].VALUE === '' ||  data[area][row][col.repayItem06].VALUE === '[]') ||
              (!data[area][row][col.repayItem05].VALUE || data[area][row][col.repayItem05].VALUE === '' ||  data[area][row][col.repayItem05].VALUE === '[]'))) {
              // 还款单类型返回SR02 
              flag = false;
              repaySR01 = true;
              break;
            }
          }
        }
      }
    }
    if (!flag) {
      if (lendType) {
        showMessageBox('请填写组织机构和预算科目，请检查！');
      } else if (lendTypeN) {
        showMessageBox('组织机构、预算科目和关联动支单不可填写，请检查！');
      } else if (repaySR01) {
        showMessageBox('还入银行名称，银行账号，还入机构，现金流必须填写，核算科目不可填写，请检查！');
      } else if (repaySR02) {
        showMessageBox('还入银行名称，银行账号，还入机构，现金流不可填写，核算科目必须填写，请检查！');
      } else {
        showMessageBox('单据中必填项未填，请检查！');
      }
    }
    return flag;
  },
  // 公共方法
  // 主表区金额大于0
  public01(data) {
    const mainMoney = data.main && col.mainMoney;
    if (parseFloat(mainMoney) === 0) {
      return false;
    }
    return true;
  },
  // 凭证区凭证时间必须保持一致
  public02(data) {
    const voucher = data.voucher;
    if (voucher && voucher.length && voucher[0][col.voucherDate]) {
      const date = voucher[0][col.voucherDate].LABEL;
      for (const row of voucher) {
        if (row[col.voucherDate].LABEL !== date) {
          showMessageBox(`${getAreaName('voucher')} 的 ${getColumnName(col.voucherDate)} 必须保持一致！`);
          return false;
        }
      }
    }
    return true;
  },
  // 明细区行必须有分摊
  public03(data) {
    const detail = data.detail;
    const share = data.share;
    if (share && share.length) {
      const shareConn = share.map(row => parseInt(row[col.shareConn].LABEL));
      for (const row of detail) {
        if (shareConn.indexOf(parseInt(row[col.detailNo].LABEL)) === -1) {
          showMessageBox(`${getAreaName('detail')} 第${row[col.detailNo].LABEL}行 必须进行分摊，请检查！`);
          return false;
        }
      }
    } else {
      showMessageBox(`${getAreaName('detail')} 必须进行分摊，请检查！`);
      return false;
    }
    return true;
  },
  // 申请单
  // 借款区金额=付款区实付金额
  apply01(data) {
    const lend = data.lend;
    const payment = data.payment;
    if (payment && payment.length && lend && lend.length) {
      let lendSum = 0;
      let lendKey = col.lendMoney;
      if (areaInfo.lend.AREA_COLUMNS[col.lendMoney]) {
        lendSum = sumColumn4Area(lend, col.lendMoney);
      } else if (data.main[0].main_item29 && areaInfo.lend.AREA_COLUMNS[col.lendNewMoney]) {
        lendSum = sumColumn4Area(lend, col.lendNewMoney) * data.main[0].main_item29.LABEL;
        lendKey =  col.lendNewMoney;
      }
      let paymentSum = 0;
      let paymentKey = col.payMoney;
      if (areaInfo.payment.AREA_COLUMNS[col.payMoney]) {
        paymentSum = sumColumn4Area(payment, col.payMoney);
      } else if (data.main[0].main_item29 && areaInfo.payment.AREA_COLUMNS[col.payNewMoney]) {
        paymentSum = sumColumn4Area(payment, col.payNewMoney) * data.main[0].main_item29.LABEL;
        paymentKey =  col.payNewMoney;
      }
      if (lendSum !== paymentSum) {
        showMessageBox(`${getAreaName('lend')} 的 ${getColumnName(lendKey)} 之和与 ${getAreaName('payment')} 的 ${getColumnName(paymentKey)} 之和不相等，请检查！`);
        return false;
      }
    }
    return true;
  },
  // 借款区金额小于等于主表区金额
  apply02(data) {
    const main = data.main;
    const lend = data.lend;
    if (main && lend && lend.length) {
      if (areaInfo.lend.AREA_COLUMNS[col.lendNewMoney]) {
        const mainCurrency = parseFloat(main[0][col.mainMoney].LABEL);
        const lendSum = sumColumn4Area(lend, col.lendNewMoney);
        if (lendSum > mainCurrency) {
          showMessageBox(`单据的 ${getColumnName(col.mainMoney)} 小于 ${getAreaName('lend')} 的 ${getColumnName(col.lendNewMoney)} 之和，请检查！`);
          return false;
        }
      } else {
        const mainCurrency = parseFloat(main[0][col.mainMoney].LABEL);
        const lendSum = sumColumn4Area(lend, col.lendMoney);
        if (lendSum > mainCurrency) {
          showMessageBox(`单据的 ${getColumnName(col.mainMoney)} 小于 ${getAreaName('lend')} 的 ${getColumnName(col.lendMoney)} 之和，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 明细区的申请金额大于0
  apply03(data) {
    const detail = data.detail;
    const billNo = data.main[0].main_billNo.LABEL;
    if (detail && detail.length && !billNo.includes('RP')) {
      for (const row of detail) {
        if (row[col.detailMoney]) {
          if (parseFloat(row[col.detailMoney].LABEL) <= 0) {
            showMessageBox(`单据的 ${getAreaName('detail')} 的 ${getColumnName(col.detailMoney)} 必须大于0，请检查！`);
            return false;
          }
        } else if(row[col.detailNewMoney]) {
          if (parseFloat(row[col.detailNewMoney].LABEL) <= 0) {
            showMessageBox(`单据的 ${getAreaName('detail')} 的 ${getColumnName(col.detailNewMoney)} 必须大于0，请检查！`);
            return false;
          }
        }
      }
    }
    return true;
  },
  apply04(data) {
    return this.public01(data);
  },
  // 标准区汇总=明细区汇总
  apply05(data) {
    const detail = data.detail;
    const standard = data.standard;
    if (detail && detail.length && standard && standard.length) {
      let detailSum = 0;
      if (areaInfo.detail.AREA_COLUMNS[col.detailMoney]) {
        detailSum = sumColumn4Area(detail, col.detailMoney);
      } else if (data.main[0].main_item29 && areaInfo.detail.AREA_COLUMNS[col.detailNewMoney]) {
        detailSum = sumColumn4Area(detail, col.detailNewMoney) * data.main[0].main_item29.LABEL;
      }
      const standardSum = sumColumn4Area(standard, col.standardMoney);
      if (standardSum !== detailSum) {
        showMessageBox(`${getAreaName('standard')} 的 ${getColumnName(col.standardMoney)} 之和与 ${getAreaName('detail')} 的 ${getColumnName(col.detailMoney)} 之和不相等，请检查！`);
        return false;
      }
    }
    return true;
  },
  // 明细区行必须有分摊
  apply06(data) {
    return this.public03(data);
  },
  // 重新申请单主表区币种=付款区币种
  repaymentApply01(data) {
    const main = data.main;
    const payment = data.payment;

    if (main && main.length && main[0][col.mainItem28]) {
      const date = main[0][col.mainItem28].LABEL;
      if (payment && payment.length && payment[0][col.paymentCurrency]) {
        const dateCurrency = payment[0][col.paymentCurrency].LABEL;
        if (date !== dateCurrency) {
          showMessageBox(`单据的 ${getColumnName(col.mainItem28)} 与 ${getColumnName(col.paymentCurrency)}的币种都要相等，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 报销单
  // 主表区金额=付款区应付金额汇总
  expense01(data) {
    const main = data.main;
    const payment = data.payment;
    const rever = data.rever;

    if (main && main[0][col.mainMoney]) {
      const mainCurrency = parseFloat(main[0][col.mainMoney].LABEL);
      if (payment && payment.length) {
        let totalSum = sumColumn4Area(payment, col.payTotal);
        if (totalSum !== mainCurrency) {
          showMessageBox(`单据的 ${getColumnName(col.mainMoney)} 与 ${getColumnName(col.payTotal)} 之和不相等，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 明细区行金额=对应分摊行金额汇总
  expense02(data) {
    const detail = data.detail;
    const share = data.share;
    if (detail && detail.length && share && share.length) {
      // 分摊区根据分组进行计算总和
      let shareGroup;
      if (areaInfo.share.AREA_COLUMNS[col.shareMoney]) {
        shareGroup = sumColumnGroup4Area(share, col.shareMoney, col.shareConn);
      } else if (data.main[0].main_item29 && areaInfo.share.AREA_COLUMNS[col.shareNewMoney]) {
        shareGroup = sumColumnGroup4Area(share, col.shareNewMoney, col.shareConn);
        for (const key of Object.keys(shareGroup)) {
          shareGroup[key] = shareGroup[key] * data.main[0].main_item29.LABEL;
        }
      }
      for (const row of detail) {
        const no = row[col.detailNo].LABEL;
        let currency = 0;
        if (row[col.detailMoney]) {
          currency = parseFloat(row[col.detailMoney].LABEL);
        } else if(row[col.detailNewMoney] && data.main[0].main_item29) {
          currency = parseFloat(row[col.detailNewMoney].LABEL) * data.main[0].main_item29.LABEL;
        }
        if (no && (shareGroup[no] || shareGroup[no] === 0.00) && currency !== shareGroup[no]) {
          showMessageBox(`${getAreaName('detail')} 第${no}行 ${getColumnName(col.detailMoney)} 与对应 ${getAreaName('share')} 的 ${getColumnName(col.shareMoney)} 之和不相等，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 付款区应付金额大于等于冲销金额
  expense03(data) {
    const payment = data.payment;
    const billNo = data.main[0].main_billNo.LABEL;
    if (payment && payment.length && payment[0][col.payTotal] && payment[0][col.payRever]  && !billNo.includes('RP')) {
      for (const row of payment) {
        if (parseFloat(row[col.payTotal].LABEL) < parseFloat(row[col.payRever].LABEL)) {
          showMessageBox(`${getAreaName('payment')} 第${row[col.payNo].LABEL}行的 ${getColumnName(col.payTotal)} 不能小于 ${getColumnName(col.payRever)} ，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 冲销区冲销金额小于等于关联借款单的付款行未冲销金额
  expense04(data) {
  //   const rever = data.rever;
  //   if (rever && rever.length) {
  //     for (const row of rever) {
  //       if (row[col.reverConn] && row[col.reverMoney].LABEL > row[col.reverLastMoney].LABEL) {
  //         showMessageBox(`${getAreaName('rever')} 第${row[col.reverNo].LABEL}行的 ${getColumnName(col.reverMoney)} 大于剩余可冲销金额${row[col.reverLastMoney].LABEL}，请检查！`);
  //         return false;
  //       }
  //     }
  //   }
    return true;
  },
  // 若为申请类，则明细区行金额小于等于关联明细行剩余可报销金额
  expense05(data) {
  //   const detail = data.detail;
  //   if (detail && detail.length) {
  //     for (const row of detail) {
  //       if (row[col.detailConn] && row[col.detailMoney].LABEL > row[col.dzMoney].LABEL) {
  //         showMessageBox(`${getAreaName('detail')} 第${row[col.detailNo].LABEL}行的 ${getColumnName(col.detailMoney)} 大于剩余可冲销金额${row[col.dzMoney].LABEL}，请检查！`);
  //         return false;
  //       }
  //     }
  //   }
    return true;
  },
  // 关联了同一发票明细的明细区行金额之和《=剩余可用税价合计
  expense06(data) {
  //   const detail = data.detail;
  //   const invoiceRecorde = {};
  //   if (detail && detail.length) {
  //     for (const row of detail) {
  //       if (row.invoice && row.invoice[col.invoiceDid] && row[col.detailMoney].LABEL) {
  //         const id = row.invoice[col.invoiceDid];
  //         if (!invoiceRecorde[id]) {
  //           invoiceRecorde[id] = parseFloat(row.invoice[col.invoiceLeftTotal]);
  //         }
  //         const left = floatSum(invoiceRecorde[id], -parseFloat(row[col.detailMoney].LABEL));
  //         if (left < 0) {
  //           showMessageBox(`${getAreaName('detail')} 关联的 ${row.invoice[col.invoiceName]} 的 ${row.invoice[col.invoiceDetail]} 行剩余可报销金额不足，请检查！`);
  //           return false;
  //         }
  //         invoiceRecorde[id] = left;
  //       }
  //     }
  //   }
    return true;
  },
  // 关联了同一借款单的付款行的冲销行金额之和小于等于剩余可冲销金额
  expense07(data) {
  //   const rever = data.rever;
  //   const reverRecorde = {};
  //   if (rever && rever.length) {
  //     for (const row of rever) {
  //       if (row[col.reverConn] && row[col.reverConn].LABEL) {
  //         const id = row[col.reverConn];
  //         if (!reverRecorde[id]) {
  //           reverRecorde[id] = parseFloat(row[col.reverLeftMoney].LABEL);
  //         }
  //         const left = floatSum(reverRecorde[id], -parseFloat(row[col.reverMoney].LABEL));
  //         if (left < 0) {
  //           showMessageBox(`${getAreaName('rever')} 的第${row[col.reverNo]}行剩余可冲销金额不足，请检查！`);
  //           return false;
  //         }
  //         reverRecorde[id] = left;
  //       }
  //     }
  //   }
    return true;
  },
  // 付款区合同剩余可付金额大于等于应付金额
  expense08(data) {
    const payment = data.payment;
    if (payment && payment.length && payment[0][col.payTotal] && payment[0][col.payRemain]) {
      for (const row of payment) {
        if (parseFloat(row[col.payRemain].LABEL) && parseFloat(row[col.payRemain].LABEL) < parseFloat(row[col.payTotal].LABEL)) {
          showMessageBox(`${getAreaName('payment')} 第${row[col.payNo].LABEL}行的 ${getColumnName(col.payRemain)} 不能小于 ${getColumnName(col.payTotal)} ，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 明细区行必须有分摊
  expense09(data) {
    return this.public03(data);
  },
  // 预算区的报销金额大于0
  expense10(data) {
    const detail = data.detail;
    const billNo = data.main[0].main_billNo.LABEL;
    if (detail && detail.length && !billNo.includes('RP')) {
      for (const row of detail) {
        if (row[col.detailNewMoney]) {
          if (parseFloat(row[col.detailNewMoney].LABEL) <= 0) {
            showMessageBox(`单据的 ${getAreaName('detail')} 的 ${getColumnName(col.detailNewMoney)} 必须大于0，请检查！`);
            return false;
          }
        } else {
          if (parseFloat(row[col.detailMoney].LABEL) <= 0) {
            showMessageBox(`单据的 ${getAreaName('detail')} 的 ${getColumnName(col.detailMoney)} 必须大于0，请检查！`);
            return false;
          }
        }
      }
    }
    return true;
  },
  // 标准区总金额等于明细区总金额
  expense11(data) {
    const standard = data.standard;
    const detail = data.detail;
    if (standard && standard.length && detail && detail.length) {
      let standardSum = 0;
      for (const sc of Object.keys(areaInfo.standard.AREA_COLUMNS)) {
        if (areaInfo.standard.AREA_COLUMNS[sc].COL_TYPE === 'AMOUNT') {
          standardSum += sumColumn4Area(standard, areaInfo.standard.AREA_COLUMNS[sc].COL_CODE);
        }
      }
      let detailSum = 0;
      let colstr = col.detailMoney;
      if (data.main[0].main_item29 && areaInfo.detail.AREA_COLUMNS[col.detailNewMoney]) {
        detailSum = sumColumn4Area(detail, col.detailNewMoney) * data.main[0].main_item29.LABEL;
        colstr = col.detailNewMoney;
      } else {
        detailSum = sumColumn4Area(detail, col.detailMoney);
      }
      if (standardSum !== detailSum) {
        showMessageBox(`${getAreaName('standard')} 的 所有费用 之和与 ${getAreaName('detail')} 的 ${getColumnName(colstr)} 之和不相等，请检查！`);
        return false;
      }
    }
    return true;
  },
  // 明细区关联合同 付款区必须关联付款计划
  expense12(data) {
    const detail = data.detail;
    const payment = data.payment;

    const contractCount = detail && detail.length ? detail.filter(row => row[col.detailContract].LABEL !== '').length : 0;
    const payPlanCount = payment && payment.length ? payment.filter(row => row[col.payContract].LABEL !== '').length : 0;

    if (contractCount && !payPlanCount) {
      showMessageBox(`${getAreaName('detail')} 的 ${getColumnName(col.detailContract)} 对应的 ${getAreaName('payment')} 的 ${getColumnName(col.payContract)} 为必填项，请检查！`);
      return false;
    }
    return true;
  },
  expense13(data) {
    return this.public01(data);
  },
  // 关联合同是否必填
  expense14(data) {
    const detail = data.detail;
    for (const row in detail) {
      let flag;
      const columns = areaInfo.detail.AREA_COLUMNS;
      for (const colObj in columns) {
        if (columns[colObj].COL_ISCONTRACT === 'true' && detail[row][colObj] && detail[row][colObj].contractFlag) {
          flag = detail[row][colObj].contractFlag === 'true' ? true : false;
        }
        if (flag === false) break;
      }
      if (flag && detail[row][col.detailContract] && !detail[row][col.detailContract].VALUE) {
        showMessageBox(`${getAreaName('detail')} 第${+row + 1}行 的 ${getColumnName(col.detailContract)} 为必填项，请检查！`);
        return false;
      }
    }
    return true;
  },
  expense15(data) {
    return this.public02(data);
  },
  // 借款单
  // 主表区金额=付款区实付金额汇总
  loan01(data) {
    const main = data.main;
    const payment = data.payment;
    if (main && payment && payment.length) {
      if (areaInfo.payment.AREA_COLUMNS[col.payNewMoney]) {
        const mainCurrency = parseFloat(main[0][col.mainMoney].LABEL);
        const paymentSum = sumColumn4Area(payment, col.payNewMoney);
        if (paymentSum !== mainCurrency) {
          showMessageBox(`单据的 ${getColumnName(col.mainMoney)} 与 ${getColumnName(col.payNewMoney)} 之和不相等，请检查！`);
          return false;
        }
      } else {
        const mainCurrency = parseFloat(main[0][col.mainMoney].LABEL);
        const paymentSum = sumColumn4Area(payment, col.payMoney);
        if (paymentSum !== mainCurrency) {
          showMessageBox(`单据的 ${getColumnName(col.mainMoney)} 与 ${getColumnName(col.payMoney)} 之和不相等，请检查！`);
          return false;
        }
      }
    }
    return true;
  },
  // 若为申请类单据，借款区行金额小于等于关联申请单可借款金额
  loan02(data) {
    return true;
  },
  // 借款区的借款金额大于0
  loan03(data) {
    const lend = data.lend;
    const billNo = data.main[0].main_billNo.LABEL;
    if (lend && lend.length && !billNo.includes('RP')) {
      for (const row of lend) {
        if (row[col.lendMoney]) {
          if (parseFloat(row[col.lendMoney].LABEL) <= 0) {
            showMessageBox(`单据的 ${getAreaName('lend')} 的 ${getColumnName(col.lendMoney)} 必须大于0，请检查！`);
            return false;
          }
        } else if (row[col.lendNewMoney]) {
          if (parseFloat(row[col.lendNewMoney].LABEL) <= 0) {
            showMessageBox(`单据的 ${getAreaName('lend')} 的 ${getColumnName(col.lendNewMoney)} 必须大于0，请检查！`);
            return false;
          }
        }
      }
    }
    return true;
  },
  loan04(data) {
    return this.public01(data);
  },
  loan05(data) {
    return this.public02(data);
  },
  // 借款区关联合同 付款区必须关联付款计划
  loan06(data) {
    const lend = data.lend;
    const payment = data.payment;

    const contractCount = lend && lend.length ? lend.filter(row => row[col.lendContract].LABEL !== '').length : 0;
    const payPlanCount = payment && payment.length ? payment.filter(row => row[col.payContract].LABEL !== '').length : 0;

    if (contractCount && !payPlanCount) {
      showMessageBox(`${getAreaName('lend')} 的 ${getColumnName(col.lendContract)} 对应的 ${getAreaName('payment')} 的 ${getColumnName(col.payContract)} 为必填项，请检查！`);
      return false;
    }
    return true;
  },
  // 还款单
  // 还款区金额=冲销区金额
  repayment01(data) {
    const rever = data.rever;
    const repay = data.repay;
    if (rever && repay) {
      let reverSum = 0;
      let reverKey = col.reverMoney;
      if (areaInfo.rever.AREA_COLUMNS[col.reverMoney]) {
        reverSum = sumColumn4Area(rever, col.reverMoney);
      } else if (data.main[0].main_item29 && areaInfo.rever.AREA_COLUMNS[col.reverNewMoney]) {
        reverSum = sumColumn4Area(rever, col.reverNewMoney);
        reverKey = col.reverNewMoney;
      }
      let repaySum = 0;
      let repayKey = col.repayMoney;
      if (areaInfo.repay.AREA_COLUMNS[col.repayMoney]) {
        repaySum = sumColumn4Area(repay, col.repayMoney);
      } else if (data.main[0].main_item29 && areaInfo.repay.AREA_COLUMNS[col.repayNewMoney]) {
        repaySum = sumColumn4Area(repay, col.repayNewMoney);
        repayKey = col.repayNewMoney;
      }
      if (!(reverSum === repaySum)) {
        showMessageBox(`单据的 ${getColumnName(reverKey)} 与 ${getColumnName(repayKey)}之和不相等，请检查！`);
        return false;
      }
    }
    return true;
  },
  // 冲销区行金额小于等于关联付款行未冲销金额
  repayment02(data) {
    return true;
  },
  repayment03(data) {
    return this.public01(data);
  },
  repayment04(data) {
    return this.public02(data);
  },
  // 预算调整单
  // 主表区金额为0
  adjust01(data) {
    const main = data.main;
    if (main && main.length) {
      if (parseFloat(main[0][col.mainMoney].LABEL) !== 0) {
        showMessageBox('单据金额不为零 ，请检查！');
        return false;
      }
    }
    return true;
  },
};

export default function validation(data = {}, area = {}, valiArray = []) {
  areaInfo = area;
  if (!validate.validationRequired(data)) {
    return false;
  }
  if (valiArray.length) {
    for (const ruleName of valiArray) {
      if (validate[ruleName] && !validate[ruleName](data)) {
        return false;
      }
    }
  }
  return true;
}
